﻿using Microsoft.Win32;
using System;
using System.Management;
//需引用System.Management程序集

namespace CC.Common
{
    public class ComputerInfo
    {
        public string CpuId { get; }
        public string MacAddress { get; }
        public string IpAddress { get; }
        public string DiskId { get; }
        public string UserName { get; }
        public string SystemType { get; }
        public long TotalPhysicalMemory { get; }
        public string ComputerName { get; }
        public string OsName { get; }
        public string OsVersion { get; }
        public ComputerInfo()
        {
            ComputerName = Environment.MachineName;
            OsVersion = Environment.OSVersion.ToString();
            RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
            OsName = rk.GetValue("ProductName").ToString();//产品名称（系统名称）
            try
            {
                using (ManagementClass mc = new ManagementClass("Win32_Processor"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        CpuId = mo.Properties["ProcessorId"].Value.ToString();//cpu序列号                                        
                    }
                }
                if (string.IsNullOrEmpty(CpuId)) CpuId = "Unknown";
            }
            catch (Exception)
            {
                CpuId = "Unknown";
            }
            try
            {
                using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {                        
                        UserName = mo["UserName"].ToString();
                        SystemType = mo["SystemType"].ToString();
                        TotalPhysicalMemory = Convert.ToInt64(mo["TotalPhysicalMemory"]);
                    }
                }
                if (string.IsNullOrEmpty(UserName)) UserName = "Unknown";
                if (string.IsNullOrEmpty(SystemType)) SystemType = "Unknown";
            }
            catch (Exception)
            {
                UserName = "Unknown";
                SystemType = "Unknown";
                TotalPhysicalMemory = 0;
            }
            try
            {
                using (ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        DiskId = (string)mo.Properties["SerialNumber"].Value;
                        break;
                    }
                }
                if (string.IsNullOrEmpty(DiskId)) DiskId = "Unknown";
            }
            catch (Exception)
            {
                DiskId = "Unknown";
            }
            
            try
            {
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            Array addres = (Array)(mo.Properties["IpAddress"].Value);
                            IpAddress += addres.GetValue(0).ToString() + " ";
                            MacAddress += mo["MacAddress"].ToString() + " ";
                        }
                    }
                }
                if (string.IsNullOrEmpty(IpAddress)) IpAddress = "Unknown";
                if (string.IsNullOrEmpty(MacAddress)) MacAddress = "Unknown";
            }
            catch (Exception)
            {
                IpAddress = "Unknown";
                MacAddress = "Unknown";
            }
        }

        public static string GetCpuID()
        {
            try
            {
                //获取CPU序列号代码 
                string cpuInfo = "";//cpu序列号 
                using (ManagementClass mc = new ManagementClass("Win32_Processor"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    }
                }
                return string.IsNullOrEmpty( cpuInfo)? "Unknown":cpuInfo;
            }
            catch
            {
                return "Unknown";
            }
        }
        public static string GetMacAddress()
        {
            try
            {
                //获取网卡硬件地址 
                string mac = "";
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            mac += mo["MacAddress"].ToString()+"  ";                            
                        }
                    }
                }
                return string.IsNullOrEmpty(mac) ? "Unknown" : mac;
            }
            catch
            {
                return "Unknown";
            }
        }
        public static string GetIPAddress()
        {
            try
            {
                //获取IP地址 
                string st = "";
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            Array ar;
                            ar = (Array)(mo.Properties["IpAddress"].Value);
                            st += ar.GetValue(0).ToString()+"  ";
                        }
                    }
                }
                return string.IsNullOrEmpty(st) ? "Unknown" : st;
            }
            catch
            {
                return "Unknown";
            }
        }
        public static string GetDiskID()
        {
            try
            {
                //获取硬盘ID 
                String HDid = "Unknown";
                using (ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        HDid = (string)mo.Properties["SerialNumber"].Value;
                        break;

                    }
                }               
                return string.IsNullOrEmpty(HDid) ? "Unknown" : HDid;
            }
            catch
            {
                return "Unknown";
            }
        }
        /// <summary> 
        /// 操作系统的登录用户名 
        /// </summary> 
        /// <returns></returns> 
        public static string GetUserName()
        {
            try
            {
                string st = "";
                using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        st = mo["UserName"].ToString();
                    }
                }
                return string.IsNullOrEmpty(st) ? "Unknown" : st; 
            }
            catch
            {
                return "Unknown";
            }
        }
        /// <summary> 
        /// PC类型 
        /// </summary> 
        /// <returns></returns> 
        public static string GetSystemType()
        {
            try
            {
                string st = "";
                using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        st = mo["SystemType"].ToString();
                    }
                }
                return string.IsNullOrEmpty(st) ? "Unknown" : st;
            }
            catch
            {
                return "Unknown";
            }
        }

        /// <summary> 
        /// 获取物理内存大小
        /// </summary> 
        /// <returns></returns> 
        public static long GetTotalPhysicalMemory()
        {
            long mem = 0;
            try
            {
                using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        mem = Convert.ToInt64(mo["TotalPhysicalMemory"]);
                    }
                }
            }
            catch
            {
                mem = 0;
            }
            return mem;
        }
        /// <summary> 
        ///  
        /// </summary> 
        /// <returns></returns> 
        public static string GetComputerName()
        {
            try
            {
                return Environment.MachineName;
            }
            catch
            {
                return "Unknown";
            }
        }
        public  static string GetOsVersion()
        {
            return Environment.OSVersion.ToString();
        }
        public static string GetOsName()
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
                string osName = rk.GetValue("ProductName").ToString();//产品名称（系统名称）
                string serverpack = rk.GetValue("CSDVersion").ToString();//seserver pack版本号
                string productID = rk.GetValue("ProductId").ToString();//系统激活序列号
                string buildNumber = rk.GetValue("CurrentBuildNumber").ToString();//系统版本号
                rk.Close();
                return osName;
            }
            catch (Exception)
            {
                return "Unknown";
            }
           
            
        }
    }
}
